package com.itheima1.itheima.homework3;

import java.util.concurrent.locks.ReentrantLock;

public class Ticket implements Runnable {
    private int ticket = 100;
    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {


        while (true) {
            try {
                lock.lock();
                if (ticket == 0) {
                    System.out.println("票已卖完");
                    return;
                } else {
                    ticket--;
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "已售出一张票,还剩" + ticket + "张");
                }
            } finally {
                lock.unlock();
            }
        }
    }
}

